﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class ucDmBacSy : BaseUserControl
    {
        private string sql = "";
        private DAL.AccessData m;
        private DataTable dt = new DataTable();
        public ucDmBacSy(DAL.AccessData acc)
        {
            InitializeComponent();
            m = acc;
            this.Title = "DM Bác sỹ";
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            hoten.Text = "";
            ena_button(false);
            ena_object(true);
            mabs.Text = m.get_capid_mabs.ToString().PadLeft(4,'0');
            hoten.Focus();
        }
        private void ena_object(bool ena)
        {
            hoten.Enabled = ena;
            cboKp.Enabled = ena;
            cboNhom.Enabled = ena;
            chkNghiviec.Enabled = ena;
        }
        private void ena_button(bool ena)
        {
           
            //butKetthuc.Enabled = ena;
        }
        private void butLuu_Click(object sender, EventArgs e)
        {
            if (hoten.Text == "")
            {
                hoten.Focus(); return;
            }
            if (mabs.Text != "")
            {
                m.upd_dmbs(mabs.Text, hoten.Text,cboNhom.SelectedIndex.ToString() + cboNhom.Text, (chkNghiviec.Checked) ? 1 : 0);
                load_grid();
                ena_button(true);
                ena_object(false);
                //reft_text();
                
            }
        }
        private void load_grid()
        {
            sql = "select a.ma,a.hoten,substr(a.nhom,2) as nhom,substr(a.nhom,1,1) as manhom,a.nghiviec from " + m.user + ".dmbs a order by a.ma";
            dt = m.get_data(sql).Tables[0];
            
            this.gridControl1.DataSource = dt;
        }
        private void AddGridTableStyle()
        {
            
        }
        
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        private void butSua_Click(object sender, EventArgs e)
        {
            ena_button(false);
            ena_object(true);
            hoten.Focus();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            ena_button(true);
            ena_object(false);
            hoten.Enabled = false;
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (mabs.Text != "" && hoten.Text != "")
            {
                if (kiemtra(mabs.Text))
                {
                    MessageBox.Show("Danh mục đã sử dụng, không được huỷ!", DAL.AccessData.Msg); return;
                }
                if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    sql = "delete from " + m.user + ".dmbs where ma='" + mabs.Text + "'";
                    m.execute_data(sql);
                    load_grid();
                }
            }
        }
        private bool kiemtra(string ma)
        {
            sql = "select * from " + m.user + ".benhanpk where mabs='" + ma + "'";
            if (m.get_data(sql).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
            ena_object(false);
        }
        private void reft_text()
        {
            try
            {
                //mabs.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString();
                //hoten.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
                ////cboKp.SelectedValue = dataGrid1[dataGrid1.CurrentCell.RowNumber, 4].ToString();
                //cboNhom.SelectedIndex = int.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 5].ToString());
                //chkNghiviec.Checked = dataGrid1[dataGrid1.CurrentCell.RowNumber, 6].ToString() == "1";
            }
            catch { }
        }

        private void cboNhom_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void frmDmbs_Load(object sender, EventArgs e)
        {
            //cboKp.DisplayMember = "TENKP";
            //cboKp.ValueMember = "MAKP";
            //cboKp.DataSource = m.get_data("select * from "+m.user+".btdkp order by makp").Tables[0];
            this.ShowExportButtons = true;
            this.ShowSearch = true;
            load_grid();
            AddGridTableStyle();
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void crudOperation1_ButtonClick(object sender, DBOperateEventArgs e)
        {
            switch (e.ButtonKind) { 
                case ButtonType.Delete:
                    butHuy_Click(null, null); break;
                case ButtonType.Edit:
                    butSua_Click(null, null); break;
                case ButtonType.Ignore:
                    butBoqua_Click(null, null); break;
                case ButtonType.New:
                    butMoi_Click(null, null); break;
                case ButtonType.Save:
                    butLuu_Click(null, null); break;
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            var dataRow = this.gridView1.GetDataRow(e.RowHandle);
            mabs.Text = dataRow["ma"].ToString();
            hoten.Text = dataRow["hoten"].ToString();
            ////cboKp.SelectedValue = dataGrid1[dataGrid1.CurrentCell.RowNumber, 4].ToString();
            //cboNhom.SelectedIndex = int.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 5].ToString());
            //chkNghiviec.Checked = dataGrid1[dataGrid1.CurrentCell.RowNumber, 6].ToString() == "1";
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            var dataRow = this.gridView1.GetDataRow(e.RowHandle);
            mabs.Text = dataRow["ma"].ToString();
            hoten.Text = dataRow["hoten"].ToString();
        }

        public override void CaptionButtonClick(object sender, CaptionEventArgs e)
        {
            if (e.ButtonKind == CaptionButtonKind.Search)
            {
                MessageBox.Show("Search button clicked with text " + e.SearchText);
            }
            else if (e.ButtonKind == CaptionButtonKind.ExportExcel)
                MessageBox.Show("Export excel button clicked");
            else
                MessageBox.Show("Export pdf button clicked");
        }
    }
}